﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sahit.iSoilHotel.Portal.ServiceProxy.Tavern.Models.Room;
using Sahit.iSoilHotel.Infrastructure.Web.Caching;
using Sahit.iSoilHotel.Portal.ServiceProxy.Tavern.Models.Filter;
using Sahit.iSoilHotel.Portal.ServiceProxy.Tavern.Models.Record;
using Sahit.iSoilHotel.Portal.ServiceProxy.Tavern.Proxys.Room;

namespace Sahit.iSoilHotel.Portal.Controllers
{
    public class RoomController : Controller
    {
        #region Members

        private readonly HttpContextBase httpContext;
        private readonly ITavernRoomServiceProxy tavernRoomServiceProxy;
        private readonly ICacheManager cacheManager;

        #endregion

        #region Constructors

        public RoomController(HttpContextBase httpContext,
            ITavernRoomServiceProxy tavernRoomServiceProxy,
            ICacheManager cacheManager)
        {
            this.httpContext = httpContext;
            this.tavernRoomServiceProxy = tavernRoomServiceProxy;
            this.cacheManager = cacheManager;
        }

        #endregion

        public ActionResult Index()
        {
            List<RoomModel> models = this.tavernRoomServiceProxy.GetRoom(null);
            var query = 
                from m in models
                select new RoomViewModel(){ID = m.ID, StatusID = m.StatusID, Number = m.Number, RoomType = m.RoomType};
            List<RoomViewModel> viewModels = query.ToList();

            List<BuildingModel> buildings = this.tavernRoomServiceProxy.GetAllBuildings();
            ViewBag.Buildings = buildings;
            return View(viewModels);
        }

        [HttpGet]
        public JsonResult SearchRoom(int? roomStatusID, Guid? roomTypeID, string roomNumber, Guid? buildingID, int? floorID)
        {
            RoomFilterModel filter = new RoomFilterModel() { RoomNumber = roomNumber, RoomTypeID = roomTypeID, RoomStatusID = roomStatusID, BuildingID = buildingID, FloorID=floorID };
            return SearchRoom(filter);
        }

        // [HttpGet]
        //public JsonResult SearchRoom()
        //{
        //    RoomFilterModel filter = new RoomFilterModel() { BuildingID = buildingID, FloorID = floorID};
        //    return SearchRoom(filter);
        //}

         private JsonResult SearchRoom(RoomFilterModel filter)
         {
             List<RoomModel> rooms = this.tavernRoomServiceProxy.GetRoom(filter);
             var roomTypesResult = new JsonResult();
             roomTypesResult.Data = rooms;
             roomTypesResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
             return roomTypesResult;
         }
        [HttpGet]
        public JsonResult GetFloors(Guid buildingID)
        {
            var floors = this.tavernRoomServiceProxy.GetFloors(buildingID);

            var floorsResult = new JsonResult();
            floorsResult.Data = floors;
            floorsResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return floorsResult;
        }
    }
}
